<!--新增采购单界面移动到这个界面上来@鲍小伟20251127-->
<style scoped lang="scss">
@import url('add.scss');
</style>
<template>
    <div class="app-container">
      <div class="toptitle title">
        {{title}}
      </div>
      <el-form ref="purchaseRef" :model="form" :rules="rules" label-width="100px">

        <el-row :gutter="20" class="title">
          <el-col :span="12">
            基础信息
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="采购单号" prop="purchaseno">
              <el-input v-model="form.purchaseno" placeholder="请输入采购单号" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="申请时间" prop="purchaseDate">
              <el-input v-model="form.purchaseDate" placeholder="请输入申请时间" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="采购单位" prop="purchaseUnit">
              <el-input v-model="form.purchaseUnit" placeholder="请输入采购单位" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="采购员" prop="purchaseUser">
              <el-input v-model="form.purchaseUser" placeholder="请输入采购员" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" class="title">
          <el-col :span="12">
            <el-form-item label="合同名称" prop="contractNo">
              <el-input v-model="form.contractNo" placeholder="请输入采购合同名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同号" prop="contractNo">
              <el-input v-model="form.contractNo" placeholder="请输入采购合同号" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row :gutter="20" class="title">
          <el-col :span="12">
            厂家信息
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="生产厂家" prop="supplierId">
              <el-input v-model="form.supplierId" placeholder="请选择生产厂家" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产批次" prop="supplierPici">
              <el-input v-model="form.supplierPici" placeholder="请输入生产批次" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row :gutter="20" class="title">
          <el-col :span="12">
            药品明细
          </el-col>
        </el-row>

        <el-row :gutter="20" class="title">
          <el-col :span="24">
            <!--采购药物明细表-->


            <!--采购药物明细表end-->
          </el-col>
        </el-row>


        <el-row :gutter="20" class="title">
          <el-col :span="12">
            合同条款
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="到货时间" prop="arriveDate">
          <el-date-picker clearable v-model="form.arriveDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择要求到货时间">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="支付币种" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入币种" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" placeholder="请输入支付方式" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="合同总价" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入合同总价" />
        </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :rows="4" />
        </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>
            <el-form-item>
              <el-button class="newButton"  @click="submitForm">确定</el-button>
              <el-button @click="cancel">重置</el-button>
            </el-form-item>
      </el-form>
    </div>
</template>

<script setup name="Purchase">
import { listPurchase, getPurchase, delPurchase, addPurchase, updatePurchase } from "@/api/system/purchase"
import { useRouter } from 'vue-router'
const router = useRouter()

const { proxy } = getCurrentInstance()

const purchaseList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("新增采购单")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    purchaseno: null,
    supplierId: null,
    supplierPici: null,
    contractNo: null,
    purchaseUser: null,
    purchaseDate: null,
    arriveDate: null,
    currency: null,
    totalAmount: null,
    status: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询采购单列表 */
function getList() {
  loading.value = true
  listPurchase(queryParams.value).then(response => {
    purchaseList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    purchaseid: null,
    purchaseno: null,
    supplierId: null,
    supplierPici: null,
    contractNo: null,
    purchaseUser: null,
    purchaseDate: null,
    arriveDate: null,
    currency: null,
    totalAmount: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("purchaseRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.purchaseid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  //不用弹出，用一个新的界面
//  reset()
 // open.value = true
 // title.value = "添加采购单"
 router.push({
    path: '/medicine/purchase/add'
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _purchaseid = row.purchaseid || ids.value
  getPurchase(_purchaseid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改采购单"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["purchaseRef"].validate(valid => {
    if (valid) {
      if (form.value.purchaseid != null) {
        updatePurchase(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
           router.back()
        //   //关闭当前界面
        //   router.push({
        //     path: '/medicine/purchase'
        //   })
          getList()
        })
      } else {
        addPurchase(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
                  //关闭当前界面
            router.back()
          router.push({
            path: '/medicine/purchase',
            query: { t: Date.now() }
          })

          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _purchaseids = row.purchaseid || ids.value
  proxy.$modal.confirm('是否确认删除采购单编号为"' + _purchaseids + '"的数据项？').then(function() {
    return delPurchase(_purchaseids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/purchase/export', {
    ...queryParams.value
  }, `purchase_${new Date().getTime()}.xlsx`)
}

getList()
</script>
